package com.fit.pages.sold_product;

import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.services.PropertyAccess;
import org.apache.tapestry5.util.EnumSelectModel;

import com.fit.annotations.AdminAccess;
import com.fit.annotations.UserAccess;
import com.fit.dao.manufacturer.ManufacturerDAO;
import com.fit.dao.product.ProductDAO;
import com.fit.dao.sold_product.SoldDAO;
import com.fit.dao.user.UserDAO;
import com.fit.entities.Sold_product;
import com.fit.entities.Manufacturer;
import com.fit.entities.Product;
import com.fit.entities.User;
import com.fit.model.glov.GenericSelectModel;
import com.fit.model.type.ProductType;
import com.fit.model.type.UserType;
import com.fit.model.user.MD5;

@AdminAccess
@UserAccess
public class Sell {

	@Property
	@SessionState(create = false)
	private Sold_product sold_product;
	
	@Inject
	private SoldDAO soldDAO;
	
	@Inject
	private Messages messages;

	@Inject
	private PropertyAccess access;

	@Persist
	@Property
	private Product product;
	
	@Persist
	@Property
	private User user;

	@Inject
	private ProductDAO productDAO;
	
	@Inject
	private UserDAO userDAO;
	
	@Component
	private Form createForm;

	@Persist
	@Property
	private String error;
	
	@Persist
	@Property
	private String confirmation;
	
	@Property
	@Persist
	private Long sold;

	private GenericSelectModel<User> users; // lista koja ce da se
	private GenericSelectModel<Product> products;												// prikaza u cb

	void onPrepare() {
		sold_product = new Sold_product();
		sold_product.setProduct(product);
		sold_product.setUser(user);
		if(sold_product.getProduct()==null) {
			Long l = new Long("3");
			sold_product.setProduct(productDAO.findById(l));
		}
		if(sold_product.getUser()==null) {
			String s = new String();
			sold_product.setUser(userDAO.findByUsername(s));
		}
	}

	// geter za kolekciju koja se prikazuje

	public GenericSelectModel<User> getUsers() { 
		users = new GenericSelectModel<User>(userDAO.findAll(),
				User.class, "username", "id", access);
		return users;
	}
	
	public GenericSelectModel<Product> getProducts() { 
		products = new GenericSelectModel<Product>(productDAO.findAll(),
				Product.class, "model", "name", access);
		return products;
	}

//	public SelectModel getTypes() {
//		return new EnumSelectModel(ProductType.class, messages);
//	}

	
	Object onSubmitFromCreateForm() {
		error="";
		confirmation="";
		Sold_product temp = soldDAO.findById(sold_product.getId());
		if (temp == null) {
			
			sold_product.setProduct(product);
			sold_product.setUser(user);
			
			if(product.getItems_left()-sold_product.getItems_sold()<0){
				
//				product.set
				error = "Not enough products for sale!";
				return null;
			}else{
				product.setItems_left(product.getItems_left() - sold_product.getItems_sold());
				product.setItems_left(product.getTotal_sold() + sold_product.getItems_sold());
			}
			
			
			if(sold_product.getProduct()==null) {
				Long l = new Long("3");
				sold_product.setProduct(productDAO.findById(l));
			}
			
			if(sold_product.getUser()==null) {
				String s = new String();
				sold_product.setUser(userDAO.findByUsername(s));
			}
			
			if (soldDAO.save(sold_product)) {
				confirmation = "Product successfully added!";
				return null;
			}
			confirmation = "Product is null!";
			return null;
		}
		error = "Something went wrong!";
		return null;
	}
	
	@OnEvent(component = "ResetConfLink")
	Object onResetConfLink() {
		confirmation="";
		return null;
	}
	
	@OnEvent(component = "ResetErrorLink")
	Object onResetErrorLink() {
		error="";
		return null;
	}
	
	
	public boolean isErrorNotNull(){
		if(error==null || error==""){
			return false;
		}
		return true;
	}
	
	public boolean isConfirmationNotNull(){
		if(confirmation==null || confirmation==""){
			return false;
		}
		return true;
	}

}
